/*
-------------------------------------------------------------------
BIE is Copyright 2001-2003 Brunswick Corp.
-------------------------------------------------------------------
Please read the legal notices (docs/legal.txt) and the license
(docs/bie_license.txt) that came with this distribution before using
this software.
-------------------------------------------------------------------
*/
/**
 *  Data bean representing configuration of a particular sbie client.
 */
package com.webdeninteractive.sbie.config.data;

import java.lang.StringBuffer;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class ClientData {

    String name;
    String classname;
    boolean startup = false;
    //ArrayList params = new ArrayList();
    HashMap params = new HashMap();
    ArrayList services = new ArrayList();
    String protocolHandlerName;
    String protocolHandlerClassName;

    public boolean isValid() {
        return ( name!=null &&
                 classname!=null &&
                 protocolHandlerName!=null &&
                 protocolHandlerClassName!=null );
    }

    public void setProtocolHandlerName( String name ) {
        this.protocolHandlerName = name;
    }

    public String getProtocolHandlerName() {
        return this.protocolHandlerName;
    }

    public void setProtocolHandlerClassName( String name ) {
        this.protocolHandlerClassName = name;
    }

    public String getProtocolHandlerClassName() {
        return this.protocolHandlerClassName;
    }

    public void setClientName( String name ) {
        this.name = name;
    }

    public String getClientName() {
        return this.name;
    }

    public void setClientClassName( String classname ) {
        this.classname = classname;
    }

    public String getClientClassName() {
        return this.classname;
    }

    public void setStartup( boolean startup ) {
        this.startup = startup;
    }

    /** @return true if this Client should be started by the Boot System. */
    public boolean isStartup() {
        return this.startup;
    }

    /** Adds a parameter, as a Parameter object. */
    public void addParameter( Parameter p ) {
        params.put( p.getName(), p );
    }

    /** @return Collection of Parameter objects. */
    public Collection getParameters() {
        return params.values();
    }

    /** Set the parameter with the given name to the given value. */
    public void setParameter( String name, String value ) {
        if ( value!=null ) {
            Parameter p = new Parameter();
            p.setName(name);
            p.setValue(value);
            params.put( name, p );
        } else {
            params.remove( name );
        }
    }

    /** @return the value of the parameter with the given name,
     *          or null if no such parameter is set for this Client. */
    public String getParameter( String name ) {
        Parameter p = (Parameter)params.get(name);
        if (p!=null) {
            return p.getValue();
        } else {
            return null;
        }
    }

    public void addService( ServiceData s ) {
        services.add(s);
    }

    /** @return Collection of ServiceData objects. */
    public Collection getServices() {
        return (Collection)services;
    }

    /** Delete the list of services from this ClientData. */
    public void clearServices() {
        services = new ArrayList();
    }

    public String toString() {
        StringBuffer ret = new StringBuffer();
        ret.append("<client name=\"" + name + "\">\n" );
        ret.append(" <class>" + classname + "</class>\n" );
        ret.append(" <protocolhandler name=\"" + protocolHandlerName +"\">\n");
        ret.append("   <class>" + protocolHandlerClassName + "</class>\n");
        ret.append(" </protocolhandler>\n");
        ret.append(" <startup>" + startup + "</startup>\n" );
        Iterator pit = params.values().iterator();
        while(pit.hasNext()) {
            ret.append( " " + pit.next() );
        }
        Iterator sit = services.iterator();
        while(sit.hasNext()) {
            ret.append( sit.next() );
        }
        ret.append("</client>\n");

        return ret.toString();
    }
}
